﻿using CCWin;
using CCWin.SkinControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace ProductIntory.View
{
    public partial class Form1 : CCSkinMain
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static readonly HttpClient client = new HttpClient();
        public async Task GetDataFromApi(string name, string password)
        {
            try
            {
                // 发送GET请求
                var response = await client.GetAsync($"http://localhost:5279/api/Logines/LoginShow?User_Name={name}&User_PassWord={password}");
                response.EnsureSuccessStatusCode(); // 确保请求成功

                // 读取响应内容
                var responseBody = await response.Content.ReadAsStringAsync();

                if (responseBody.Contains("登录成功"))
                {
                    MessageBox.Show("登录成功");
                    this.Hide();
                    new Form1().Show();
                }
                else
                {
                    MessageBox.Show("用户名或密码错误！");
                }

                //// 反序列化为对象
                //var user = JsonSerializer.Deserialize<UserModles>(responseBody);

                //// 更新UI（注意需要在UI线程）
                //this.Invoke(new Action(() => {
                //    txtname.Text = user.UserName;
                //    txtpassword.Text = user.Password;
                //}));
            }
            catch (HttpRequestException e)
            {
                MessageBox.Show($"请求错误: {e.Message}");
            }
        }
 

        private void loginButton_Click(object sender, EventArgs e)
        {


            // 设置初始占位文本
    
            string name = txtusername.Text.Trim();
            string password = txtpassword.Text.Trim();

            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))
            {
                MessageBox.Show("用户名和密码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            GetDataFromApi(name, password);
        }

        private void restart_Click(object sender, EventArgs e)
        {
            txtusername.Text = "";
            txtpassword.Text = "";
        }

        private void SetSkinTextBoxPlaceholder(SkinTextBox skinTextBox, string placeholderText)
        {
            // 设置初始占位文本
            if (string.IsNullOrEmpty(skinTextBox.Text))
            {
                skinTextBox.Text = placeholderText;
                skinTextBox.ForeColor = Color.Gray;
            }
         
            // 获得焦点时的处理
            skinTextBox.Enter += (sender, e) =>
            {
                txtpassword.IsPasswordChat = '*';
                if (skinTextBox.Text == placeholderText && skinTextBox.ForeColor == Color.Gray)
                {
                   
                    
                    skinTextBox.Text = "";
                    skinTextBox.ForeColor = Color.Black; // 可根据皮肤样式调整为合适的颜色
                }
            };

            // 失去焦点时的处理
            skinTextBox.Leave += (sender, e) =>
            {
            
                if (string.IsNullOrEmpty(skinTextBox.Text))
                {
                
                    ///关闭
                    skinTextBox.Text = placeholderText;
                    skinTextBox.ForeColor = Color.Gray;
                }
            };
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SetSkinTextBoxPlaceholder(txtusername, "请输入用户名");
            SetSkinTextBoxPlaceholder(txtpassword, "请输入密码");
        }
    }
}
